#include <stdlib.h>
#include <stdio.h>
#include <nds.h>

// common headers
#include "common.h"

// local headers

#include "main.h"

#define _VERSION_INT MAKE_VERSION(0, 0, 1, STATE_NORMAL, 0)
char _VERSION_STR[VERSION_STR_MAX_LEN+1];

sLibProc *libhello=NULL;
void (*say_hello_in_lib)(char *)=NULL;

int init() {	// returning 1 in this function doesn't call the deinit functions !
	std->sprintf(_VERSION_STR, VERSION_FORMAT, VERSION_ARGS(_VERSION_INT));	// make version str

	std->iprintf("\x1b[2J");	// clear screen
	std->iprintf("[pluginhello-lib]init\n");

	// get hello library
	libhello=api->newLibInstance(api->getLibraryById(42/* the id i put in libhello */).lib);
	if(!libhello) {
		std->iprintf("Can't open hello library !\n");
		return 1;
	}

	say_hello_in_lib=libhello->query(QUERY_FSTRUCT);
	if(!say_hello_in_lib) {
		std->iprintf("Can't get the say_hello function pointer !\n");
		api->deleteLibInstance(libhello);
		return 1;
	}

	return 0;
}

u32 query(u32 query) {
	std->iprintf("[pluginhello-lib]query(%d)\n", query);

	switch(query) {
	case QUERY_NAME:		// plugin name
		return (u32)"helloworld-lib";
	case QUERY_VERSION_INT:	// plugin integer version
		return (u32)_VERSION_INT;
	case QUERY_VERSION_STR:	// plugin string version
		return (u32)_VERSION_STR;
	case QUERY_AUTHOR:		// plugin author
		return (u32)"Ludo6431";
	case QUERY_FILEEXT:		// plugin file extension handled
		return (u32)NULL;
	default:
		return 0;
	}
}

int main(int argc, char *argv[]) {
	std->iprintf("[pluginhello-lib]Hello World !\n");

	say_hello_in_lib((char *)query(QUERY_NAME));

	return 0;
}

void deinit() {
	std->iprintf("[pluginhello-lib]deinit\n");

	if(libhello) api->deleteLibInstance(libhello);
}

